﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;

namespace BeatingMole
{
    class CBusGameResult
    {
        private ContentManager _content;
        private GameTime _gameTime;
        private SpriteBatch _spriteBatch;

        public CInfoGameResult _gameResult;
        private CInfoMap _map;
        private float _delayTime;
        private bool _isEnable = false;
        private MouseState MouseCur;
        private MouseState MousePre;
        private string _strScore = "";              // Chuỗi điểm màn chơi

        public CBusGameResult(ContentManager content, SpriteBatch spriteBatch, GameTime gameTime)
        {
            _content = content;
            _spriteBatch = spriteBatch;
            _gameTime = gameTime;
            _map = new CInfoMap(_content, _spriteBatch, _gameTime);
            _gameResult = new CInfoGameResult(_content);
        }

        /// <summary>
        /// Update Game Result
        /// </summary>
        /// <param name="gameTime"></param>
        public void Update(GameTime gameTime)
        {
            _delayTime += gameTime.ElapsedGameTime.Milliseconds;
            if (_gameResult.TotalScore < _map.RequiredScore)
            {                                                
                _gameResult.TotalScore = (int)CGlobalVariable.Score;
                _gameResult.ListEnemy = CGlobalVariable._listInfoEnemy;

                #region "Quy đổi điểm ra ngôi sao"
                if (!_isEnable)
                {
                    // Điểm quy đổi ra sao
                    float _score = 0;
                    if (CGlobalVariable.Score != 0)
                    {
                        _score = (CGlobalVariable.InfoPlayer.Score / _gameResult.TotalScore) * 100;
                    }
                    if (_score <= 35)
                    {
                        _gameResult.NumberFrames = 0;
                        _isEnable = true;
                    }
                    else if (_score <= 70 && _score > 35)
                    {
                        _gameResult.NumberFrames = 1;
                        _isEnable = true;
                    }
                    else if (_score > 70 && _score <= 100)
                    {
                        _gameResult.NumberFrames = 2;
                        _isEnable = true;
                    }
                    else if (_score >= 100)
                    {
                        _gameResult.NumberFrames = 3;
                        _isEnable = true;
                    }
                    else
                    {
                        _gameResult.NumberFrames = 1;
                        _isEnable = true;
                    }
                    if (_gameResult.IndexFrame >= 3)
                    {
                        _gameResult.IndexFrame = 0;
                    }
                    _gameResult.IndexFrame++;
                }
                #endregion

                #region "Hiển thị điểm người chơi"                
                _strScore = _gameResult.TotalScore.ToString().Trim();
                #endregion

                // Kiểm tra nhấn tiếp tục level khác
                NextLevel();
            }
        }

        private void NextLevel()
        {
            MouseCur = Mouse.GetState();
            if (MouseCur.LeftButton == ButtonState.Pressed && MousePre.LeftButton == ButtonState.Released)
            {
                Rectangle _recBtnNextLevel = new Rectangle(464, 300, _gameResult.ButtonNextLevel.Width, _gameResult.ButtonNextLevel.Height);
                if (new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, 1, 1).Intersects(_recBtnNextLevel))
                {
                    CGlobalVariable.level++;
                    //CGlobalVariable.NewBusMap = new CBusMap(_content, _spriteBatch, _gameTime); 
                }
            }
            MousePre = MouseCur;
        }

        /// <summary>
        /// Draw Game Result screen
        /// </summary>
        /// <param name="spriteBatch"></param>
        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(_gameResult.BackGround, Vector2.Zero, Color.Black * 0.50f);                        

            for (int i = 0; i < _gameResult.NumberFrames; i++)
            {
                spriteBatch.Draw(_gameResult.StarsImage, new Vector2(260, 140), _gameResult.XmlStars["stars" + i.ToString()], Color.White);
            }

            // Hiển thị "Score"
            spriteBatch.Draw(_gameResult.ScoreImage, new Vector2(275, 250), _gameResult.XmlScore["score_str"], Color.White);
            
            int _positionScore = 390;
            // Hiển thị số điểm
            for (int i = 0; i < _strScore.Length; i++)
            {
                if (i == 0)
                {
                    _positionScore += 30;
                }
                spriteBatch.Draw(_gameResult.ScoreImage, new Vector2(_positionScore += 30, 250), _gameResult.XmlScore["score_" + _strScore[i].ToString()], Color.White);                              
            }

            // Button Next Level
            spriteBatch.Draw(_gameResult.ButtonNextLevel, new Vector2(464, 300), Color.White);

        }
    }
}
